<?php
namespace app\admin\controller;
use think\Request;
use app\common\model\Area as AreaModel;
use app\common\model\DogFamily as DogFamilyModel;
use app\common\model\AdoptSq as AdoptSqModel;
class Adopt extends \app\admin\controller\Base {
    protected $res = array(
        'index'   => array('css' => '2,3,4', 'js' => 'list,My97DatePicker'),
        'column'   => array('css' => '2,3,4', 'js' => 'echart,My97DatePicker'),

    );
    public function index(){
        $AreaModel = new AreaModel();
        $AdoptSqModel = new AdoptSqModel();
        $search = input('get.search/a');
        $page = input('get.page');
        $search = $this->search($search, $page);
        $list = $AdoptSqModel->relData(15,$search);
        $action = '/' . Request::instance()->controller() . '/' . Request::instance()->action();
        $wherePro['level'] = 1;
        if($search[4]!=''){
            $whereCity['pid'] = $search[4];
        }else{
            $whereCity['pid'] = 2;
        }
        if($search[5]!=''){
            $whereDistrict['pid'] = $search[5];
        }else{
            $whereDistrict['pid'] = 52;
        }
        $province =$AreaModel->selectArea($wherePro);
        $city =$AreaModel->selectArea($whereCity);
        $district =$AreaModel->selectArea($whereDistrict);
        $this->assign(
            array(
                'action'=>$action,
                'list'=>$list,
                'page'=>$page,
                'search' => $search,
                'proposer_long_time'=>$AdoptSqModel::$proposer_long_time,
                'proposer_experience'=>$AdoptSqModel::$proposer_experience,
                'proposer_petyet'=>$AdoptSqModel::$proposer_petyet,
                'province' => $province,
                'city' => $city,
                'district' => $district,
            )
        );
        return $this->fetch();
    }

    /***
     * @return \think\response\Json
     * excel导出
     */
    public function excelDownload(){
        $AdoptSqModel = new AdoptSqModel();
        $search = input('post.search/a');
        $list = $AdoptSqModel->excelData($search);
        $result = array();
        foreach ($list as $key=>$vo){
            //领养状态
            if($vo['dog_adopt_status']==1){
                $result[$key]['status']='取消领养';
            }elseif($vo['dog_adopt_status']==2){
                $result[$key]['status']='待领养';
            }elseif ($vo['dog_adopt_status']==3){
                $result[$key]['status']='完成领养';
            }else{
                $result[$key]['status']='重新领养';
            }
            //申请时间
            $proposer_datetime = strtotime($vo['proposer_datetime']);
            $result[$key]['proposer_datetime'] = date('Y',$proposer_datetime).'/'.date('m',$proposer_datetime).'/'.date('d',$proposer_datetime).' '.date('H',$proposer_datetime).':'. date('i',$proposer_datetime);//注册日期
            //现居住地
            $result[$key]['address']=$vo['province'].$vo['city'].$vo['district'];
            //已在本市居住时间
            if($vo['proposer_long_time']==1){
                $result[$key]['proposer_long_time'] ='0-12个月';
            }elseif ($vo['proposer_long_time']==2){
                $result[$key]['proposer_long_time'] ='1-3年';
            }elseif ($vo['proposer_long_time']==3){
                $result[$key]['proposer_long_time'] ='4-6年';
            }else{
                $result[$key]['proposer_long_time'] ='7年以上';
            }
            //养狗经验
            if($vo['proposer_experience']==1){
                $result[$key]['proposer_experience'] ='无';
            }elseif ($vo['proposer_experience']==2){
                $result[$key]['proposer_experience'] ='少于1年';
            }elseif ($vo['proposer_experience']==3){
                $result[$key]['proposer_experience'] ='1-3年';
            }else{
                $result[$key]['proposer_experience'] ='3年以上';
            }
            //同住人员
            $result[$key]['cohabit'] = $vo['cohabit'];
            //居住环境
            $result[$key]['proposer_environment_other'] = $vo['proposer_environment_other'];
            //房屋
            $result[$key]['proposer_house_other'] = $vo['proposer_house_other'];
            //家中是否有宠物
            if($vo['proposer_petyet']==1){
                $result[$key]['petyet']='有';
            }else{
                $result[$key]['petyet']='没有';
            }
            //领养原因
            $result[$key]['reason_name'] = $vo['reason_name'];
            //谁负责照顾狗狗
            $result[$key]['care_name'] = $vo['care_name'];
            //其他情况说明
            $result[$key]['proposer_other'] = $vo['proposer_other'];
            //狗狗ID
            $result[$key]['dog_id'] = $vo['udf_id'];
            //狗狗的状态
            if($vo['udf_condition']==1){
                $result[$key]['dog_status'] ='待救助流浪狗';
            }elseif ($vo['proposer_experience']==2){
                $result[$key]['dog_status'] ='已救助流浪狗';
            }elseif ($vo['udf_condition']==3){
                $result[$key]['dog_status'] ='家养宠物';
            }else{
                $result[$key]['dog_status'] ='不详';
            }

            //狗狗的年龄
            if($vo['udf_age']==1){
                $result[$key]['dog_age'] ='不满12个月';
            }elseif ($vo['udf_age']==2){
                $result[$key]['dog_age'] ='1-3岁';
            }elseif ($vo['udf_age']==3){
                $result[$key]['dog_age'] ='4-6岁';
            }elseif($vo['udf_age']==4){
                $result[$key]['dog_age'] ='7岁以上';
            }else{
                $result[$key]['dog_age'] ='不详';
            }

            //狗狗的性别
            if($vo['udf_sex']==1){
                $result[$key]['dog_sex'] ='公';
            }else{
                $result[$key]['dog_sex'] ='母';
            }

            //狗狗的体型

            if($vo['udf_size']==1){
                $result[$key]['dog_size'] ='小型犬';
            }elseif ($vo['udf_size']==2){
                $result[$key]['dog_size'] ='中型犬';
            }else{
                $result[$key]['dog_size'] ='大型犬';
            }

            //狗狗的狂犬病免疫
            if($vo['udf_dog_vaccine']==1){
                $result[$key]['dog_vaccine'] ='否';
            }elseif($vo['udf_dog_vaccine']==2){
                $result[$key]['dog_vaccine'] ='是';
            }else{
                $result[$key]['dog_vaccine'] ='不详';
            }
            //狗狗的绝育
            if($vo['udf_dog_sterilization']==1){
                $result[$key]['dog_sterilization'] ='否';
            }elseif($vo['udf_dog_sterilization']==2){
                $result[$key]['dog_sterilization'] ='是';
            }else{
                $result[$key]['dog_sterilization'] ='不详';
            }

            //狗狗的残疾
            if($vo['udf_dog_disability']==1){
                $result[$key]['dog_disability'] ='否';
            }elseif($vo['udf_dog_disability']==2){
                $result[$key]['dog_disability'] ='是';
            }else{
                $result[$key]['dog_disability'] ='不详';
            }
            //申请用户ID
            $result[$key]['user_id'] =$vo['proposer_user_id'];

            //申请用户的性别
            if($vo['user_gender']==1){
                $result[$key]['user_sex'] ='男';
            }else if($vo['user_gender']==2){
                $result[$key]['user_sex'] ='女';
            }else{
                $result[$key]['user_sex'] ='';
            }
            //申请用户年龄
            if($vo['user_card']!=''){
                $result[$key]['user_age'] =$this->getAgeByID($vo['user_card']);
                $result[$key]['user_address'] = $this->userCard(substr($vo['user_card'],0,2));

            }else{
                $result[$key]['user_age'] = '';
                $result[$key]['user_address'] = '';

            }
            //设备
            if($vo['equipment']!=''){
                $result[$key]['equipment'] =$vo['equipment'];
            }else{
                $result[$key]['equipment'] ='';
            }
            //发布人ID
            $result[$key]['f_user_id'] =$vo['ud_d_userId'];

        }
        $xlsCell  = array(
            array('status','领养状态'),
            array('proposer_datetime','申请时间'),
            array('address','现居住地'),
            array('proposer_long_time','已在本市居住时间'),
            array('proposer_experience','养狗经验'),
            array('cohabit','同住人员'),
            array('proposer_environment_other','居住环境'),
            array('proposer_house_other','房屋'),
            array('petyet','家中是否有宠物'),
            array('reason_name','领养原因'),
            array('care_name','谁负责照顾狗狗'),
            array('proposer_other','其他情况说明'),
            array('dog_id','狗狗ID'),
            array('dog_status','狗狗的状况'),
            array('dog_age','狗狗的年龄'),
            array('dog_sex','狗狗的性别'),
            array('dog_size','狗狗的体型'),
            array('dog_vaccine','狂犬病免疫'),
            array('dog_sterilization','绝育'),
            array('dog_disability','残疾'),
            array('user_id','申请用户ID'),
            array('user_sex','申请用户性别'),
            array('user_age','申请用户年龄'),
            array('user_address','户口所在地'),
            array('equipment','设备'),
            array('f_user_id','发布用户ID'),
        );
        $data = $this->exportExcel($xlsCell,$result);
        return $data;
    }
    /**
     * @return \think\response\Json
     * 删除
     */
    public function del(){
        $ids = strtoarray(input('post.ids'));
        $AdoptSqModel = new AdoptSqModel();
        $where['proposer_id'] = array('In', $ids);
        $result = $AdoptSqModel->where($where)->delete();
        if ($result) {
            return $this->api_success('操作成功');
        } else {
            return $this->api_dbError();
        }
    }

    /**
     * 查询市
     */
    public function city(){
        $city = $_POST['city'];
        $cityArray = Db::table('up_area')->where('pid',$city)->select();
        $data['cityArray'] = $cityArray;
        return json($data);
    }
    /**
     * 查询区
     */
    public function area(){
        $area = $_POST['area'];
        $cityArray = Db::table('up_area')->where('pid',$area)->select();
        $data['cityArray'] = $cityArray;
        return json($data);
    }

    /**
     * 领养管理柱状统计页面
     */
//    public function column(){
//        $AreaModel = new AreaModel();
//        $search = input('get.search/a');
//        $page = input('get.page');
//        $search = $this->search($search, $page);
//        $action = '/' . Request::instance()->controller() . '/' . Request::instance()->action();
//        $wherePro['level'] = 1;
//        if($search[4]!=''){
//            $whereCity['pid'] = $search[4];
//        }else{
//            $whereCity['pid'] = 2;
//        }
//        if($search[5]!=''){
//            $whereDistrict['pid'] = $search[5];
//
//        }else{
//            $whereDistrict['pid'] = 52;
//        }
//        $province =$AreaModel->selectArea($wherePro);
//        $city =$AreaModel->selectArea($whereCity);
//        $district =$AreaModel->selectArea($whereDistrict);
//        $this->assign(
//            array(
//                'action'=>$action,
//                'page'=>$page,
//                'search' => $search,
//                'province' => $province,
//                'city' => $city,
//                'district' => $district,
//            )
//        );
//        return $this->fetch();
//    }

    /**
     * @return \think\response\Json
     * 领养申请管理 柱状图
     */
//    public function echartsColumn(){
//        $search = input('post.search/a');
//        $DogFamilyModel = new DogFamilyModel();
//        $AdoptSqModel = new AdoptSqModel();
//
//        $totalDog = $DogFamilyModel->countNum(1);//总的狗狗
//        $proposer_status_total = $AdoptSqModel->totalNum($search);//总的领养狗狗申请总和
//
//        $data  = $AdoptSqModel->branch($search);
//        $proposer_status1 = $AdoptSqModel->totalNum($search,1);//待领养数量
//        $proposer_status1_P =number_format($proposer_status1/$proposer_status_total,2)*100;//待领养的比例
//        $proposer_status2 = $AdoptSqModel->totalNum($search,2);//完成领养数量
//        $proposer_status2_P =number_format($proposer_status2/$proposer_status_total,2)*100;//完成领养的比例
//
//        $proposer_status3 = $AdoptSqModel->totalNum($search,3);//取消领养数量
//        $proposer_status3_P =number_format($proposer_status3/$proposer_status_total,2)*100;//取消领养的比例
//
//        $proposer_status4 = $AdoptSqModel->totalNum($search,4);//重新领养数量
//        $proposer_status4_P =number_format($proposer_status4/$proposer_status_total,2)*100;//重新领养的比例
//        $total = number_format($proposer_status_total/$totalDog,2)*100;//登记狗狗的占比
//        $res = array(
//            'proposer_status1'=>$proposer_status1,
//            'proposer_status1_P'=>$proposer_status1_P,
//            'proposer_status2'=>$proposer_status2,
//            'proposer_status2_P'=>$proposer_status2_P,
//            'proposer_status3'=>$proposer_status3,
//            'proposer_status3_P'=>$proposer_status3_P,
//            'proposer_status4'=>$proposer_status4,
//            'proposer_status4_P'=>$proposer_status4_P,
//            'total'=>$total
//            );
//        $result = array();
//        foreach ($data as $key=>$vo){
//            $result['time'][]=$vo['proposer_date'];
//            $result['value'][]=$vo['value'];
//        }
//        $data = array('totalArray'=>$result,'res'=>$res,'status'=>200);
//        return json($data);
//    }
}